<template>
  <div>
    <Menu menuActive="/download" />
    <layout isFlex>
      <template v-slot:main>
        <el-row>
          <el-col>
            <div class="top_filter_container height_light_bgcolor">
              <div style="text-align: right;">
                <el-button type="primary" :icon="Plus"
                  @click="() => { router.push({ path: '/upload-source', }) }">{{ $t("downloadPage.publishBtn") }}
                </el-button>
              </div>
              <div class="filter_search_wrap">
                <span class="filter_name">搜索：</span>
                <div class="filter_search" style="width:500px">
                  <el-input placeholder="请输入资源名称" class="input-with-select" v-model="searchParams.title" clearable
                    @clear="handleClearSearch">
                    <template #append>
                      <el-button :icon="Search" @click="handleClickSearch" />
                    </template>
                  </el-input>
                </div>
              </div>
              <div class="first_fiter_wrap">
                <span class="filter_name" style="margin-top:0">方向：</span>
                <el-tag class="cursor_class tag_item" size="large"
                  :effect="currentIndexDirect === index && directId === item.id ? 'dark' : 'light'"
                  :type="currentIndexDirect === index && directId === item.id ? '' : 'info'"
                  v-for="(item, index) in directionData" :key="item.id" @click="handleClickTagDirect(index, item)">{{
                      item.name
                  }}</el-tag>
              </div>
              <div class="second_fiter_wrap">
                <span class="filter_name" style="margin-top:0">格式：</span>
                <el-tag :effect="fileTypeIndex === index && fileTypeString === item.name ? 'dark' : 'light'"
                  :type="fileTypeIndex === index && fileTypeString === item.name ? '' : 'info'"
                  v-for="(item, index) in fileType" :key="item.id" size="large" class="cursor_class tag_item"
                  @click="handleClickFileType(index, item)">{{ item.name }}</el-tag>
              </div>
            </div>
            <div class="bottom_content_container height_light_bgcolor">
              <div class="tab_wrapper">
                <el-tabs v-model="activeName" @tab-click="handleClick">
                  <el-tab-pane :label="item.label" :name="item.name" v-for="item in tabPanel" :key="item.id">
                    <div class="tab_pane_wrapper" :style="{ padding: listLoading ? '45px 0' : '' }">
                      <div v-loading="listLoading" element-loading-text="正在玩命加载中...">
                        <File v-for="item in listData.data.value" :key="item.id" :title="item.title"
                          :format="item.format" :description="item.description" :id="item.id" :createTime="item.createTime"/>
                        <el-empty description="暂无数据~" v-if="!listLoading && listData.total.value <= 0" />
                        <div class="pagination_container">
                          <el-pagination v-model:current-page="searchParams.pageNum"
                            v-model:page-size="searchParams.pageSize" background
                            layout="total, prev, pager, next, jumper" :total="listData.total.value"
                            v-if="listData.total.value > 0" @current-change="handleCurrentChange" />
                        </div>
                      </div>
                    </div>
                  </el-tab-pane>
                </el-tabs>
              </div>
            </div>
          </el-col>
        </el-row>
      </template>
    </layout>
    <Footer />
  </div>
</template>

<script lang="ts" setup>
import Menu from '@/components/Menu/index.vue'
import Layout from '@/components/Layout/index.vue'
import Footer from '@/components/Footer/index.vue'
import Article from '@/components/Article/index.vue'
import File from '@/components/File/index.vue'
import router from '@/router'
import { onMounted, reactive, ref, toRefs, watch } from 'vue'
import type { TabsPaneContext } from 'element-plus'
import { getDirection } from "@/api/dictionary";
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
  Search,
  Plus
} from '@element-plus/icons-vue'
import { getResourceList } from '@/api/download'
import i18n from '@/i18n'
const activeName = ref<any>('first')
const value = ref<string[]>([])
const currentIndexDirect = ref<number>(0)
const directId = ref<number | null>(null)
const directionData = ref<any[]>([])
const fileType = ref<any>([{
  id: 0,
  name: '全部'
}, {
  id: 6,
  name: 'zip'
},
{
  id: 7,
  name: 'rar'
},
{
  id: 8,
  name: '7z'
}, {
  id: 1,
  name: 'doc'
}, {
  id: 2,
  name: 'xls'
}, {
  id: 4,
  name: 'txt'
}, {
  id: 5,
  name: 'pdf'
},])
const tabPanel = ref([{
  id: 1,
  name: 'first',
  label: '综合'
  // label: i18n.global.t('indexTitle.articleTitle2')
}, {
  id: 2,
  name: 'second',
  label: '最新'
  // label: i18n.global.t('indexTitle.articleTitle3')
}, {
  id: 3,
  name: 'third',
  label: '热门'
  // label: i18n.global.t('indexTitle.articleTitle4')
}])
const fileTypeIndex = ref<number>(0)
const fileTypeString = ref<string | null>('全部')
const searchParams = reactive<any>({
  pageNum: 1,
  pageSize: 10,
  title: ''
})
const listData = toRefs(reactive<any>({
  data: [],
  total: 0
}))
const listLoading = ref<boolean>(false)
const handleClick = (tab: TabsPaneContext, event: Event) => {
  activeName.value = tab.props.name
  searchParams.pageNum = 1
  activeName.value = tab.props.name
  if (activeName.value === 'first') {
    getResourceListData()
  } else if (activeName.value === 'third') {
    getResourceListData({ isHot: 1 })
  } else if (activeName.value === 'second') {
    getResourceListData({ searchValue: 1 })
  }
  // console.log(tab, event)
}
const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClickTagDirect = async (index: number, obj: any) => {
  currentIndexDirect.value = index
  directId.value = obj.id
  activeName.value = 'first'
  getResourceListData()
}
// 获取全部方向
const getDirectionData = async () => {
  const { data }: any = await getDirection()
  directionData.value = data
  // 判断路由是否有参数
  const { dirId } = router.currentRoute.value.query
  if (dirId) {
    let x = directionData.value.findIndex((item) => item.id == dirId)
    currentIndexDirect.value = x
    directId.value = Number(dirId)
    return
  }
  directId.value = data[0].id
}
const handleClickFileType = (index: number, obj: any) => {
  fileTypeIndex.value = index
  fileTypeString.value = obj.name
  activeName.value = 'first'
  getResourceListData()
}
/**
 * 获取资源列表
 */
const getResourceListData = async (cate?: any) => {
  try {
    listLoading.value = true
    let format = null
    if (fileTypeString.value !== '全部') {
      format = fileTypeString.value
    }
    const { rows, total }: any = await getResourceList({ ...searchParams, ...cate, directionId: directId.value, format })
    listData.data.value = rows
    listData.total.value = total
  } finally {
    listLoading.value = false
  }
}
// 搜索
const handleClickSearch = () => {
  searchParams.pageNum = 1
  activeName.value = 'first'
  getResourceListData()
}
// 清除搜索
const handleClearSearch = () => {
  searchParams.pageNum = 1
  searchParams.title = ''
  activeName.value = 'first'
  getResourceListData()
}
const handleCurrentChange = (val: number) => {
  searchParams.pageNum = val
  getResourceListData()
}
onMounted(() => {
  getDirectionData()
  getResourceListData()
})

</script>

<style scoped lang="scss">
// @import '@/assets/style/reset.scss';
@import './index.scss';
</style>
